package com.rtz.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 提供一些正则验证
 * 
 * @author ksflying
 *
 */
public class RegularVerificationUtil {

	
	
	
	
	/**
	 * 整数位最多十位，小数为最多为两位，可以无小数位
	 * 
	 * @param money
	 * @return
	 */
	public static boolean money(String money) {
		String numberRegular = "^(([0-9]|([1-9][0-9]{0,9}))((\\.[0-9]{1,2})?))$";
		return match(numberRegular, money);
	}
	/**
	 * 不以0开头的数字
	 * 
	 * @param number
	 * @return
	 */
	public static boolean onlyNumber(String number) {

		String numberRegular = "^[1-9][0-9]*$";
		return match(numberRegular, number);
	}

	/**
	 * 日期格式检验 yyyyMMdd
	 * 
	 * @param date
	 * @return
	 */
	public static boolean date(String date) {

		String numberRegular = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
		return match(numberRegular, date);
	}

	/**
	 * 验证是否为纯数字
	 * 
	 * @param number
	 * @return
	 */
	public static boolean number(String number) {
		String numberRegular = "^[0-9]*$";
		return match(numberRegular, number);
	}
	
	public static boolean IsWholeNumber(String number) {
		String numberRegular = "^(0|[1-9]\\d{0,9})$";
		return match(numberRegular, number);
	}
	
	public static boolean Is4DecimalNumber(String number) {
		String numberRegular = "^[0-9]+(.[0-9]{1,4})?$";
		return match(numberRegular, number);
	}

	/**
	 * 验证密码是否符合规则
	 * 
	 * @param password
	 * @return
	 */
	public static boolean pword(String password) {
		String pwordRegular = "^([A-Z]|[a-z]|[0-9]|[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]){6,20}$";
		return match(pwordRegular, password);
	}
	/**
	 * 验证手机号
	 * 
	 * @param puser_phone
	 * @return
	 */
	public static boolean phone(String puser_phone) {
		String phoneRegular = "^[0-9]{11}$";
		return match(phoneRegular, puser_phone);
	}

	/**
	 * 验证身份证号
	 * 
	 * @param puser_idcardno
	 * @return
	 */
	public static boolean IDNumber(String idcard_no) {
		String IDNumberRegular = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
		return match(IDNumberRegular, idcard_no);
	}

	/**
	 * 验证名字
	 * 
	 * @param puser_name
	 * @return
	 */
	public static boolean name(String puser_name) {
		String nameRegular = "^[\u4E00-\u9FA5A-Za-z]+$";
		return match(nameRegular, puser_name);
	}

	/**
	 * 验证银行卡号
	 * 
	 * @param card_no
	 * @return
	 */
	public static boolean card_no(String card_no) {
		String cardRegular = "^\\d{19}$";
		return match(cardRegular, card_no);
	}
	/**
	 * @param regex
	 *            正则表达式字符串
	 * @param str
	 *            要匹配的字符串
	 * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
	 */
	private static boolean match(String regex, String str) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	/**
	 * 隐藏部分手机号码
	 * 
	 * @param idCard
	 * @return
	 */
	public static String hiddenIdCard(String idCard) {

		return idCard.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2");
	}
	/**
	 * 隐藏部分身份证号码
	 * 
	 * @param phone
	 * @return
	 */
	public static String hiddenPhone(String phone) {

		return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
	}

	public static void main(String[] args) {

		System.out.println(
				RegularVerificationUtil.phone("12345678910"));

	}

}
